var userId=-1;
var divId="";
var flag=true;
var editUserMenu = {
    init : function() {
    	userId = getUrlParam("userId");
    	divId="usermenu"+userId;
        // 初始化事件绑定
    	editUserMenu.evenBind();
    	editUserMenu.loadUserMenu();

    },
    evenBind : function() {
        // ----------------------------------------------------------------------->
        // 设置用户菜单表单操作
        // ----------------------------------------------------------------------->
        // 取消
        $('#cancelSetUserMenu').bind('click', function() {
        	editUserMenu.closeDiv();
        });

        // 提交
        $('#submitSetUserMenu').bind('click', function() {
        	if(flag){
	    		flag=false;
	    		var $userId = $('#menuUserId').val();
	            var checkedObjs = $("input[type=checkbox][name=id]:checked");
	            var menuIds = [];
	            var i = 0;
	            if (checkedObjs.size() > 0) {
	                checkedObjs.each(function() {
	                    var v = $(this).val();
	                    menuIds[i++] = v;
	                });
	            }

	            ajaxProcess({
	                url :getProjectUrl("/bfs/menu/usermenu/add"),
	                data : {
	                    menuIds : menuIds,
	                    userId : $userId
	                },
	                requestType : 'POST',
	                traditional : true,
	                async:true,
	                successHandle : function(data) {
	                	flag=true;
	                    editUserMenu.closeDiv();
	                }
	            });
        	}
        });
    },  
    closeDiv : function() {
        do{
           window.parent.$('#'+divId).dialog('destroy');
           var win=window.parent.$('#'+divId);
        }while(win);
    },
    refreshData : function() {
    	editUserMenu.loadUserMenu();
    },
    beforeLoad : function() {
        $('#createUserDiv').dialog('destroy', true);
        $('#editUserDiv').dialog('destroy', true);
        $('#setUserMenuDiv').dialog('destroy', true);
        $('#setUserResourceDiv').dialog('destroy', true);
    },
    // ----------------------------------------------------------------------->
    // 打开设置用户菜单的窗口
    // ----------------------------------------------------------------------->
    loadUserMenu : function() {
                // $("#setUserMenuGrid").treegrid("clearChecked");
                $('#setUserMenuGrid').treegrid({
                    url : getProjectUrl('/bfs/menu/usermenu/list'),
                    queryParams : {
                        userId : userId
                    },
                    idField : 'id',
                    treeField : 'text',
                    columns : [ [ {
                        field : 'id',
                        title : 'ID',
                        width : 100,
                        checkbox : true
                    }, {
                        field : 'text',
                        title : '菜单名称',
                        width : 180,
                        align:'left'
                    } ] ],
                    singleSelect : false,
                    rownumbers : false,
                    method : 'POST',
                    fitColumns : true,
                    animate: true,
                    checkbox : false,
                    checkOnSelect : false,
                    onClickRow : function(row) {
                        var $row = $("input[type=checkbox][name=id][value=" + row.id + "]")[0];
                        $row.checked = !$row.checked;
                        nodeClick($('#setUserMenuGrid'),$row);
                    },
                    onLoadSuccess : function(row, data) {
                    	uncheckAll();
                        $("input[type=checkbox][name=id]").click(function() {
                        	nodeClick($('#setUserMenuGrid'),this);
                        });
                        checkedMenu(data);
                    }
                });
                $('#menuUserId').val(userId);       
    }
};
$(function() {
    resetSize();
    editUserMenu.init();
    resetSize();
});

